package br.com.andreikeda.utility.gestor.finance;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;

import br.com.andreikeda.utility.gestor.R;
import br.com.andreikeda.utility.gestor.finance.fragment.CategoriesFragment;
import br.com.andreikeda.utility.gestor.finance.listener.OnUpdateUIListener;
import br.com.andreikeda.utility.gestor.finance.storage.database.DatabaseHelper;
import br.com.andreikeda.utility.gestor.finance.storage.preferences.Preferences;
import br.com.andreikeda.utility.gestor.finance.utils.DatabaseUtils;
import butterknife.InjectView;


public class MainActivity extends FragmentActivity {

    private final int STATUS_FEED = 0;
    private final int STATUS_CATEGORIES = 1;
    private final int STATUS_STATISTIC = 2;
    private final int STATUS_STOCK = 3;
    private final int STATUS_DIVIDEND = 4;

    @InjectView(R.id.drawer_layout)
    protected DrawerLayout mDrawerLayout;
    @InjectView(R.id.content_frame)
    protected FrameLayout mContentFrame;
    @InjectView(R.id.left_drawer)
    protected ListView mDrawerList;

    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;

    private int status = STATUS_CATEGORIES;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActionBar().setIcon(android.R.color.transparent);
        setContentView(R.layout.activity_main);
        if (Preferences.getInstance(this).isFirstTime()) {
            DatabaseUtils.start(this, DatabaseHelper.getInstance(this).getWritableDatabase());
            Preferences.getInstance(this).onIsFirstTimeChanged(false);
        }

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_launcher,  /* nav drawer icon to replace 'Up' caret */
                R.string.description_drawer_open,  /* "open drawer" description */
                R.string.description_drawer_closed  /* "close drawer" description */
        ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(mDrawerTitle);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // Set the adapter for the list view
        //mDrawerList.setAdapter(adapter = new DrawerMenuAdapter(this, this));

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

        selectItem(status);
    }

    private void exit() {
        //SharedPreferencesUtil.deleteUser(getApplicationContext());
        finish();
    }

    private void selectItem(int position) {
        status = position;
        Fragment fragment = null;

        switch (status) {
            case STATUS_CATEGORIES:
                fragment = new CategoriesFragment();
                break;
        }

        if (fragment == null)
            return;

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment)
                .commit();

        // Highlight the selected item, update the title, and close the drawer
        //mDrawerList.setItemChecked(status, true);
        setTitle(getResources().getStringArray(R.array.menu_titles)[status]);
        //mDrawerLayout.closeDrawer(mDrawerList);
        if (fragment instanceof OnUpdateUIListener)
            ((OnUpdateUIListener) fragment).updateUI();
    }
}
